import regeneratorRuntime from '../../utils/wxPromise.min.js'
const ctx2 = wx.createCanvasContext('runCanvas');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    moveStyle: 'left:0',
    currentIdx: 0,
    btnW: wx.getSystemInfoSync().windowWidth / 750 * 120,
    navArray: [{
      id: 0,
      name: '待开工'
    }, {
      id: 1,
      name: '施工中'
    }, {
      id: 2,
      name: '政策处理'
    }, {
      id: 3,
      name: '竣工'
    }],
    currentNavId: 0,
    IAP: '',
    page: 1,
    limit: 10
  },
  // 获取地址前缀 Interface Address Prefix
  getAddress() {
    var IAP = getApp().globalData.host;
    this.setData({
      IAP: IAP
    })
  },
  // canvas画环装图
  run(c, w, h) { //c是圆环进度百分比   w，h是圆心的坐标
    let that = this;
    var num = (2 * Math.PI / 100 * c) - 0.5 * Math.PI;
    var lineWidth = wx.getSystemInfoSync().windowWidth / 750 * 25
    //圆环的绘制
    ctx2.arc(w, h, w - lineWidth / 2, -0.5 * Math.PI, num); //绘制的动作
    ctx2.setStrokeStyle("#ffb530"); //圆环线条的颜色
    ctx2.setLineWidth(lineWidth); //圆环的粗细
    ctx2.setLineCap("round"); //圆环结束断点的样式  butt为平直边缘 round为圆形线帽  square为正方形线帽
    ctx2.stroke();
    //开始绘制百分比数字
    ctx2.beginPath();
    ctx2.setFontSize(18); // 字体大小 注意不要加引号
    ctx2.setFillStyle("#ffb530"); // 字体颜色
    ctx2.setTextAlign("center"); // 字体位置
    ctx2.setTextBaseline("middle"); // 字体对齐方式
    ctx2.fillText(c + "%", w, (h + 8)); // 文字内容和文字坐标
    ctx2.draw();
  },
  // 选择nav
  chooseNav(e) {
    if (e.currentTarget.dataset.navid == this.data.currentNavId) {
      return;
    } else {
      this.setData({
        currentNavId: e.currentTarget.dataset.navid,
        page: 1
      })
      this.getEngineeringInfo();
    }
  },
  // 触摸开始
  touchS: function(e) {
    if (e.touches.length == 1) {
      this.setData({
        moveStyle: 'left:0',
        startX: e.touches[0].clientX,
        currentIdx: e.currentTarget.dataset.idx,
      })
    }
  },
  // 触摸移动
  touchM: function(e) {
    if (e.touches.length == 1) {
      var moveX = e.touches[0].clientX;
      var disX = this.data.startX - moveX
      var moveStyle = '';
      //如果移动距离小于等于0，文本层位置不变
      if (disX <= 0) {
        moveStyle = "left:0";
      } else if (disX > 0 && disX < 2 * this.data.btnW) {
        moveStyle = "left:-" + disX + "px";
      } else if (disX > 2 * this.data.btnW) {
        moveStyle = "left:-" + (2 * this.data.btnW) + "px";
      }
      this.setData({
        moveStyle: moveStyle
      })
    }
  },
  // 触摸结束
  touchE(e) {
    if (e.changedTouches.length == 1) {
      var endX = e.changedTouches[0].clientX;
      var disX = this.data.startX - endX
      var moveStyle = '';
      //如果移动距离小于等于0，文本层位置不变
      if (disX < this.data.btnW) {
        moveStyle = "left:0";
      } else if (disX >= this.data.btnW) {
        moveStyle = "left:-" + (2 * this.data.btnW) + "px";
      }
      this.setData({
        moveStyle: moveStyle,
        startX: 0
      })
    }
  },
  // 触摸打断
  touchC(e) {
    this.setData({
      moveStyle: 'left:0',
      startX: 0
    })
  },
  // 删除工程
  deleteEngineering(e) {
    wx.showModal({
      title: '删除提醒',
      content: '是否确认删除本条记录',
      confirmColor: '#ff4c4c',
      confirmText: '删除',
      success: res => {
        if (res.confirm) {
          wx.request({
            url: this.data.IAP + 'project/delete',
            method: "POST",
            data: [e.currentTarget.dataset.idx],
            header: {
              "token": wx.getStorageSync('token')
            },
            success: res => {
              if (res.data.code == 0) {
                var newList = this.data.engineeringList.filter((value) => {
                  return value.projectId != e.currentTarget.dataset.idx
                })
                this.setData({
                  engineeringList: newList
                })
              } else {
                wx.showToast({
                  title: res.data.msg,
                  icon: 'none'
                })
              }
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  // 根据施工状态获取工程信息
  getEngineeringInfo(){
    return new Promise( (resolve, reject) => {
        wx.request({
          url: this.data.IAP + 'project/index',
          data: {
            page: this.data.page,
            limit: this.data.limit,
            status: this.data.currentNavId,
          },
          header: {
            "token": wx.getStorageSync('token')
          },
          success: res => {
            if (res.data.code == 0) {
              if (this.data.page == 1) {
                this.setData({
                  map: res.data.map,
                  engineeringList: res.data.map.page.list,
                  page: this.data.page + 1,
                })
                resolve(res.data.map.complete);
              } else {
                this.setData({
                  map: res.data.map,
                  engineeringList: this.data.engineeringList.concat(res.data.map.page.list),
                  page: this.data.page + 1
                })
                resolve(res.data.map.complete);
              }
            } else {
              wx.showToast({
                title: res.data.msg,
                icon: 'none'
              })
            }
          }
        })     
        
    });

  },
  //canvas
  canvasRun(completeC){
    console.log(completeC)
    wx.createSelectorQuery().select('#' + 'runCanvas').boundingClientRect((rect) => {
      //监听canvas的宽高
      var w = parseInt(rect.width / 2); //获取canvas宽的的一半
      var h = parseInt(rect.height / 2); //获取canvas高的一半
      var c = completeC ? this.data.map.complete : 0
      this.run(c, w, h)
    }).exec();    
  },
  async getEngineering() {
    // await this.getEngineeringInfo();
    let c = await this.getEngineeringInfo();
    await this.canvasRun(c)
  },
  // 获取通知公告
  getsysnotice() {
    wx.request({
      url: this.data.IAP + 'sysnotice/list',
      data: {
        page: 1,
        list: 6,
      },
      header: {
        "token": wx.getStorageSync('token')
      },
      success: res => {
        if (res.data.code == 0) {
          this.setData({
            sysnoticeList: res.data.page.list
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.getAddress();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow:  function() {
    this.getsysnotice();
    // setTimeout(() => {
    //   wx.createSelectorQuery().select('#' + 'runCanvas').boundingClientRect((rect) => {
    //     //监听canvas的宽高
    //     var w = parseInt(rect.width / 2); //获取canvas宽的的一半
    //     var h = parseInt(rect.height / 2); //获取canvas高的一半
    //     var c = this.data.map.complete ? this.data.map.complete : 0
    //     this.run(c, w, h)
    //   }).exec();
    // }, 1000)
    this.setData({
      moveStyle: 'left:0',
    })
    this.getEngineering();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})